package com.xudow.app.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.xudow.app.R;
import com.xudow.app.data.Pair;
import com.xudow.app.ui.BaseActivity;

import java.util.List;

/**
 * 下拉菜单Adapter
 *
 * @author lukecheng
 */
public class CourseFilterMenuAdapter extends BaseAdapter {
    private Context context;
    private int resource;
    private List<Pair> objects;
    public CourseFilterMenuAdapter(Context context, int resource, List<Pair> objects) {
        this.context=context;
        this.objects=objects;
        this.resource = resource;
    }
    public void setData(List<Pair> objects){
        this.objects=objects;
        notifyDataSetChanged();
    }
    @Override
    public int getCount() {
        if (null==objects)
        return 0;
        else
            return objects.size();
    }

    @Override
    public Pair getItem(int position) {
        return objects.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;

        if (null == convertView) {
            viewHolder = new ViewHolder();

            convertView = LayoutInflater.from(context).inflate(this.resource, null);
            viewHolder.root = (LinearLayout) convertView.findViewById(R.id.root);
            viewHolder.name = (TextView) convertView.findViewById(R.id.name);
            viewHolder.icon = (ImageView) convertView.findViewById(R.id.icon);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        Pair item = getItem(position);
        String str="";
        if (null!=item.one){
            str=item.one.getName();
        }else if (null!=item.two){
            str=item.two.getSubjectname();
        }else if(null!=item.three){
            str=item.three.getGradename();
        }else
            str=item.first;

        viewHolder.name.setText(str);

        if (item.checked) {
            viewHolder.name.setTextColor(context.getResources().getColor(R.color.rvcode));
            viewHolder.icon.setVisibility(View.VISIBLE);
        } else {
            viewHolder.name.setTextColor(context.getResources().getColor(android.R.color.secondary_text_light));
            viewHolder.icon.setVisibility(View.GONE);
        }

        //二级以下选项
        if (R.layout.item_course_filter_menu_grey == this.resource) {
            if (item.selected) {
                viewHolder.name.setTextColor(context.getResources().getColor(R.color.rvcode));
                viewHolder.icon.setVisibility(View.GONE);
            }
        }

        return convertView;
    }

    class ViewHolder {
        public LinearLayout root;
        public TextView name;
        public ImageView icon;
    }
}
